﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class MaintenanceApply : EFViewCmes
    {
        public MaintenanceApply(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    this.fdInvalid.Visibility = Visibility.Collapsed;
                    base.Mode = OperationMode.Insert;
                    break;
                case "cancel":
                    this.fdCode.Visibility = Visibility.Collapsed;
                    this.fdPerson.Visibility = Visibility.Collapsed;
                    this.fdDepartmentId.Visibility = Visibility.Collapsed;
                    this.fdEquipmentId.Visibility = Visibility.Collapsed;
                    this.fdEquipmentCode.Visibility = Visibility.Collapsed;
                    this.fdEquipmentQuestion.Visibility = Visibility.Collapsed;
                    this.fdRemarks.Visibility = Visibility.Collapsed;
                    this.fdDate.Visibility = Visibility.Collapsed;
                    base.Mode = OperationMode.Update;
                    break;
                case "update":
                    this.fdCode.IsEnabled = false;
                    this.fdPerson.IsEnabled = false;
                    this.fdDepartmentId.IsEnabled = false;
                    this.fdEquipmentId.IsEnabled = false;
                    this.fdInvalid.Visibility = Visibility.Collapsed;
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertPanel.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 设备改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null && !picker.ItemObject.Equals(""))
            {
                this.fdEquipmentCode.Value = picker.ItemObject.GetType().GetProperty("fdCode").GetValue(picker.ItemObject, null) as string;
                this.fdEquipmentId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null) as string;
            }
        }

        /// <summary>
        /// 申请单位改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            CmesCustomView equipmentPicker = this.fdEquipmentId.Content as CmesCustomView;
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdDepartmentId.Value = box.SelectedItem.GetType().GetProperty("orgCode").GetValue(box.SelectedItem, null).ToString();
                equipmentPicker.ItemObject = "";
                this.fdEquipmentCode.Value = "";
                equipmentPicker.eViewInitParams = "bindingService=ZM05.queryEquipment,columns=fdCode|fdName|fdSpecifications|fdModel|fdUsingType,queryConditions=fdCode|fdName|fdUsingType,initParam=devolve:yes|fdDepartmentId:" + this.fdDepartmentId.Value;
            }
        }

        /// <summary>
        /// 申请人改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdPerson_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null && !picker.ItemObject.Equals(""))
            {
                this.fdPerson.Value = picker.ItemObject.GetType().GetProperty("empName").GetValue(picker.ItemObject, null).ToString();
            }
        }
    }
}
